var userBar = null;
var roleBar = null;
var pwdBar = null;

document.ready(function() {
    userBar = getHtml('userBar');
    roleBar = getHtml('roleBar');
    pwdBar = getHtml('pwdBar');
    setHtml('userBar', '');
    setHtml('roleBar', '');
    setHtml('pwdBar', '');

    webSpinner.spin(getItem('loadSpin'));
    queryKey('ok');
});

function queryKey(flag) {
    var param = {};
    param.ctrl = 'account';
    param.func = 'getUserList';
    param.isuse = getAjaxValue('statusInfo');
    param.keyword = getAjaxValue('searchKeyword');

    sendAjax(param, function(result) {
        userList.list = result.userList;
        setHtml('listCount', result.listCount);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });
}

function slipsearch_clearOption() {
    //重置筛选值
    setValue('statusInfo', '');

    //重置筛选种类样式
    setClass('optionItemText1', 'option-item-text');
    getItem('optionItemimg1').src = 'static/pic/app/option_down.png';
    setHtml('optionItemText1', '状态');

    //重置筛选项样式
    var objList = getItem('optionItemFloatDiv1').children;
    for (var i = 0; i < objList.length; i++) {
        if (i == 0) {
            objList[i].getElementsByTagName('div')[0].className = 'select-text-click';
            objList[i].getElementsByTagName('img')[0].style.display = '';
        }
        else {
            objList[i].getElementsByTagName('div')[0].className = 'select-text';
            objList[i].getElementsByTagName('img')[0].style.display = 'none';
        }
    }
    setDisplay('optionItemFloatDiv1', 'none');
    setDisplay('maskOptionDiv', 'none');

    optionIndex = '0';
}

function slipedit_submit(flag) {
    if (flag == 'resetPassword') {
        savePasswordInfo();
    }
    else if (flag == 'editRole') {
        saveRoleInfo();
    }
    else if (flag == 'editUser') {
        saveUserInfo();
    }
}

function editUser(id) {
    setValue('editid', id);
    setValue('editflag', 'editUser');

    var param = {};
    param.ctrl = 'account';
    param.func = 'getUserInfo';
    param.userid = getAjaxValue('editid');

    sendAjax(param, function(result) {
        openSlipEditDiv('编辑用户', userBar);

        setValue('usermc', result.name);
        setValue('username', result.username);

        if (result.isuse == 1) {
            setClass('statusDiv', 'slipedit-info-onoff-select');
            setClass('statusBtnDiv', 'slipedit-info-onoff-btn-select');
            setValue('userstatus', '1');
        }
        else {
            setClass('statusDiv', 'slipedit-info-onoff');
            setClass('statusBtnDiv', 'slipedit-info-onoff-btn');
            setValue('userstatus', '0');
        }
    });
}

function saveUserInfo() {
    if (isEmpty('editid')) {
        showTip('ID格式错误');
        return;
    }
    if (isEmpty('usermc') || isWicked('usermc')) {
        showTip('姓名格式错误');
        return;
    }
    if (isEmpty('username') || isWicked('username')) {
        showTip('账号格式错误');
        return;
    }
    if (!isNum('userstatus')) {
        showTip('状态格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'saveUser';
    param.flag = 'edit';
    param.userid = getAjaxValue('editid');
    param.usermc = getAjaxValue('usermc');
    param.username = getAjaxValue('username');
    param.status = getAjaxValue('userstatus');

    sendAjax(param, function(result) {
        closeSlipEditDiv();
        queryKey('ok');
    });
}

function isUse() {
    if (getValue('userstatus') == '1') {
        setClass('statusDiv', 'slipedit-info-onoff');
        setClass('statusBtnDiv', 'slipedit-info-onoff-btn');
        setValue('userstatus', '0');
    }
    else {
        setClass('statusDiv', 'slipedit-info-onoff-select');
        setClass('statusBtnDiv', 'slipedit-info-onoff-btn-select');
        setValue('userstatus', '1');
    }
}

function editRole(id, name) {
    setValue('editid', id);
    setValue('editflag', 'editRole');
    openSlipEditDiv('设置角色权限：' + name, roleBar);
}

function selectRoleType() {
    openSlipSelectDiv(getValue('roleType'), getHtml('roleTypeSelectBar'));
}

function selectRoleName() {
    if (isEmpty('roleType') || isEmpty('roleTypeId')) {
        showTip('请选择角色类型');
        return;
    }

    openSlipSelectDiv(getValue('roleName'), getHtml('roleNameSelectBar'));
}

function getRoleName() {
    if (isEmpty('roleType') || isEmpty('roleTypeId')) {
        setValue('roleName', '');
        setValue('roleNameId', '');
        setHtml('roleNameSelectBar', '');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'getRoleType';
    param.typeid = getAjaxValue('roleTypeId');

    sendAjax(param, function(result) {
        setValue('roleName', '');
        setValue('roleNameId', '');

        loadSelectItemList('roleNameSelectBar', 'roleName', 'roleNameId', result.roleList);
    });
}

function saveRoleInfo() {
    if (isEmpty('editid')) {
        showTip('ID格式错误');
        return;
    }
    if (isEmpty('roleType') || !isNum('roleTypeId')) {
        showTip('角色类型格式错误');
        return;
    }
    if (isEmpty('roleName')) {
        showTip('角色名称格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'editRole';
    param.userid = getAjaxValue('editid');
    param.roleid = getAjaxValue('roleNameId');

    sendAjax(param, function(result) {
        closeSlipEditDiv();
        queryKey('ok');
    });
}

function resetUsername(id, name) {
    setValue('editid', id);
    openConfirm('确定重置 【' + name + '】 吗？', function() {
        var param = {};
        param.ctrl = 'account';
        param.func = 'retUsername';
        param.userid = getAjaxValue('editid');

        sendAjax(param, function(result) {
            closeSlipEditDiv();
            queryKey('ok');
        });
    });
}

function resetPassword(id, name) {
    setValue('editid', id);
    setValue('editflag', 'resetPassword');
    openSlipEditDiv('重置密码：' + name, pwdBar);
}

function savePasswordInfo() {
    if (isEmpty('editid')) {
        showTip('ID格式错误');
        return;
    }
    if (isEmpty('password') || isWicked('password')) {
        showTip('新密码格式错误');
        return;
    }

    var param = {};
    param.ctrl = 'account';
    param.func = 'retPassword';
    param.userid = getAjaxValue('editid');
    param.password = getAjaxValue('password');

    sendAjax(param, function(result) {
        closeSlipEditDiv();
        queryKey('ok');
    });
}